﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Home.Web.Models.Resume;
using Home.Web.Data;

namespace Home.Web.Controllers
{
    [RoutePrefix("api")]
    public class SkillController : ApiController
    {
        private readonly ResumeDbContext _db = new ResumeDbContext();

        [Route("SkillsBySkillType/{id}", Name = "SkillsBySkillType")]
        public List<Skill> GetSkillsBySkillType(int id)
        {
            return (from s in _db.Skills where s.SkillTypeId == id select s).ToList();
        }

        // GET api/Skill/5
        [Route("Skill/{id}")]
        [ResponseType(typeof(Skill))]
        public async Task<IHttpActionResult> GetSkill(int id)
        {
            Skill skill = await _db.Skills.FindAsync(id);
            if (skill == null)
            {
                return NotFound();
            }

            return Ok(skill);
        }

        // PUT api/Skill/5
        [Route("Skill/{id}", Name = "PUT Skill")]
        public async Task<IHttpActionResult> PutSkill(int id, Skill skill)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != skill.Id)
            {
                return BadRequest();
            }

            _db.Entry(skill).State = EntityState.Modified;

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SkillExists(id))
                {
                    return NotFound();
                }
                throw;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        [Route("Skill", Name = "POST Skill")]
        [ResponseType(typeof(Skill))]
        public async Task<IHttpActionResult> PostSkill(Skill skill)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _db.Skills.Add(skill);
            await _db.SaveChangesAsync();

            return CreatedAtRoute("POST Skill", new { id = skill.Id }, skill);
        }

        // DELETE api/Skill/5
        [ResponseType(typeof(Skill))]
        public async Task<IHttpActionResult> DeleteSkill(int id)
        {
            Skill skill = await _db.Skills.FindAsync(id);
            if (skill == null)
            {
                return NotFound();
            }

            _db.Skills.Remove(skill);
            await _db.SaveChangesAsync();

            return Ok(skill);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool SkillExists(int id)
        {
            return _db.Skills.Count(e => e.Id == id) > 0;
        }
    }
}